package it.stasbranger.myproject.client.managed.ui;

import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;

import com.google.gwt.text.shared.AbstractRenderer;
import com.google.gwt.text.shared.AbstractSafeHtmlRenderer;
import com.google.gwt.text.shared.Renderer;

import it.stasbranger.myproject.client.managed.request.RequestsProxy;

import it.stasbranger.myproject.client.scaffold.ScaffoldMobileApp;

import it.stasbranger.myproject.client.scaffold.ui.MobileProxyListView;

import java.util.HashSet;
import java.util.Set;

public class RequestsMobileListView extends MobileProxyListView<RequestsProxy> {

	/**
	 * The renderer used to render cells.
	 */
	private static class CellRenderer extends AbstractSafeHtmlRenderer<RequestsProxy> {
		private final String dateStyle = ScaffoldMobileApp.getMobileListResources().cellListStyle().dateProp();
		private final String secondaryStyle = ScaffoldMobileApp.getMobileListResources().cellListStyle().secondaryProp();

		private final Renderer<String> nameRenderer = new AbstractRenderer<String>() {
			public String render(String obj) {
				return obj == null ? "" : String.valueOf(obj);
			}
		};

		public SafeHtml render(RequestsProxy value) {
			if (value == null) {
				return SafeHtmlUtils.EMPTY_SAFE_HTML;
			}

			// Primary property.
			SafeHtmlBuilder sb = new SafeHtmlBuilder();

			if (value.getName() != null) {
				sb.appendEscaped(nameRenderer.render(value.getName()));
			}

			sb.appendHtmlConstant("</div>");

			// Date property.
			sb.appendHtmlConstant("<div class=\"" + dateStyle + "\">");

			sb.appendHtmlConstant("</div>");
			sb.appendHtmlConstant("</div>");

			return sb.toSafeHtml();
		}
	}

	private static RequestsMobileListView instance;

	private final Set<String> paths = new HashSet<String>();

	public static RequestsMobileListView instance() {
		if (instance == null) {
			instance = new RequestsMobileListView();
		}

		return instance;
	}

	public RequestsMobileListView() {
		super("New ", new CellRenderer());

		paths.add("idrequests");
		paths.add("date");
		paths.add("name");
		paths.add("email");
	}

	public String[] getPaths() {
		return paths.toArray(new String[paths.size()]);
	}
}
